package com.xfyr.mvpdemo.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/4/24.
 * GitHub：https://github.com/RookieExaminer
 * Email：wei.azheng@foxmail.com
 * Description：
 */
public abstract class BaseActivity extends AppCompatActivity {


    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(this.getLayoutId());
        unbinder = ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化视图
     */
    public abstract void initView();


    private LoadingPopupView loadingPopupView;


    protected void showLoadingView() {
        if (loadingPopupView == null) {
            loadingPopupView = new XPopup.Builder(this)
                    .asLoading();
        }
        loadingPopupView.show();
    }

    protected void showLoadingView(boolean b) {
        if (loadingPopupView == null) {
            loadingPopupView = new XPopup.Builder(this)
                    .dismissOnBackPressed(b) // 按返回键是否关闭弹窗，默认为true
                    .dismissOnTouchOutside(b) // 点击外部是否关闭弹窗，默认为true
                    .asLoading();

        }
        loadingPopupView.show();
    }


    protected void hideLoadingView() {
        if (loadingPopupView != null && loadingPopupView.isShow()) {
            loadingPopupView.dismiss();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    /**
     * 防止重复点击（1s点击一次）
     *
     * @param onClickListener
     * @param views
     */
    protected void oneSecoundsClick(View.OnClickListener onClickListener, View... views) {
        for (int i = 0; i < views.length; i++) {
            View view = views[i];
            RxView.clicks(view)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            onClickListener.onClick(view);
                        }
                    });
        }

    }

    /**
     * View禁止点击
     *
     * @param view
     */
    protected void viewCanNotClick(View view) {
        if (view == null) {
            return;
        }
        try {
            RxView.clickable(view).accept(false);
            RxView.enabled(view).accept(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * View恢复点击
     *
     * @param view
     */
    protected void viewRestoreClick(View view) {
        if (view == null) {
            return;
        }
        try {
            RxView.clickable(view).accept(true);
            RxView.enabled(view).accept(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.requestFocusFromTouch();
    }
}
